from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,GridSearchCV
import warnings
warnings.filterwarnings('ignore')
#获取数据
data = load_breast_cancer()
x=data.data
y=data.target

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=123)

lr = LogisticRegression()
pg={
    'C':[0.1,0.5,1,2,5,10]
}
gs = GridSearchCV(lr,param_grid=pg,cv=5)
gs.fit(x_train,y_train)
print("最优参数",gs.best_params_)
print("最优得分",gs.best_score_)

#找到超参数后 重新训练模型
lr_best = LogisticRegression(C=gs.best_params_['C'])
lr_best.fit(x_train,y_train)
y_predict=lr_best.predict(x_test)
print('y_predict')
print(y_predict)

y_predict_pro=lr_best.predict_proba(x_test)
print('y_predict_pro')
print(y_predict_pro)

y_predict_1= y_predict_pro[:,1]
print('y_predict_1')
print(y_predict_1)

#绘制ROC曲线和AUC分数(ROC曲线下方的面积，面积最大值是1)
from sklearn.metrics import roc_curve,roc_auc_score
fpr,tpr,th=roc_curve(y_test,y_predict_1)
# print("th",th)
#fpr 横坐标
#tpr 纵坐标
#th 阈值
# 每取一个阈值，会对应一个 纵坐标和一个横坐标

import matplotlib.pyplot as plt
plt.plot(fpr,tpr)
plt.show()

#AUC_SCORE
print("AUC_SCORE",roc_auc_score(y_test,y_predict_1))



#查看学习率曲线 learning curve ，重新训练模型
lr_new=LogisticRegression(C=gs.best_params_['C'])
from sklearn.model_selection import learning_curve

import numpy as np
li=list(np.linspace(0.01,1,50))
train_set_sizes,train_score,test_score=learning_curve(lr_new,x_train,y_train,cv=5,train_sizes=li)
#求 得分 的 均值 和标准差
train_score_average = np.mean(train_score,axis=1)
test_score_average = np.mean(test_score,axis=1)

train_score_std = np.std(train_score,axis=1)
test_score_std = np.std(test_score,axis=1)

#话 学习率 曲线
#先画 两条实线

plt.plot(train_set_sizes,train_score_average,"*-",c='b',label='训练集得分')
plt.plot(train_set_sizes,test_score_average,"*-",c='r',label='测试集得分')

#再画阴影部分
plt.fill_between(train_set_sizes,train_score_average-train_score_std,train_score_average+train_score_std,color='b',alpha=0.1)
plt.fill_between(train_set_sizes,test_score_average-test_score_std,test_score_average+test_score_std,color='r',alpha=0.1)

plt.show()






